angular.module('myApp',['ngRoute','ngResource'])

    .constant('SPRING','http://localhost:8080/')

//路由配置
.config(['$routeProvider',
    function ($routeProvider) {


        //$routeProvider提供路由service
        $routeProvider
            .when('/',{
                template:'this is index'
            })
            .when('/Book/:bookId', {
                templateUrl: 'book.html',
                controller: 'BookCtrl',
                controllerAs: 'book'
            })
            .when('/Book/:bookId/ch/:chapterId', {
                templateUrl: 'chapter.html',
                controller: 'ChapterCtrl',
                controllerAs: 'chapter'
            })
            .otherwise({
                redirectTo:'/'
            });
    }

])

    .factory('bookService',['SPRING','$resource',function (SPRING,$resource) {
        var Book = $resource(SPRING+'findAll',{},{read:{method:'GET',params : {}}});
        console.log(Book);
        return {book:Book};
    }])

    
//导航区控制器
.controller('navController',['$route', '$routeParams', '$location',
    function($route, $routeParams, $location) {
        this.$route = $route;
        this.$location = $location;
        this.$routeParams = $routeParams;
    }
])

    //ngResource测试控制器
    // .controller('TestCtrl',['SPRING','$scope','$resource',function (SPRING,$scope,$resource) {
    //     console.log($resource);
    //     var Book = $resource(SPRING+'findAll',{},{read:{method:'GET',params : {}}});
    //     console.log(Book);
    //     var book = Book.read({}, function(response){
    //         console.log(response);
    //     });
    // }])

    //bookCtrl
    .controller('BookCtrl', ['$scope','$routeParams','bookService', function ($scope,$routeParams,bookService) {
        $scope.name = 'BookCtrl';
        $scope.params = $routeParams;

        $scope.bookService = function () {
            bookService.book.read({}, function(response){
                $scope.books = response.content;
                console.log($scope.books)
            })
        };
    }])

    // 章节控制器
    .controller('ChapterCtrl', ['$routeParams', function($routeParams) {
        this.name = 'ChapterCtrl';
        this.params = $routeParams;
        console.log(this.params)
    }])



